<?php

namespace App\Http\Controllers\Admins\Posts;

use App\Http\Controllers\Admins\BaseAdminController;
use App\Models\OperationalLogs;
use App\Models\Posts;
use App\Models\PostsComment;
use App\Models\Users;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\View\Factory;
use Illuminate\Contracts\View\View;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class PostsCommentController extends BaseAdminController
{
    /**
     * index
     *
     * @param Request $request
     * @param $post_id
     * @return array|Application|Factory|View
     */
    public function index(Request $request,$post_id){
        $where = [
            'post_id'=>$post_id
        ];
        $where1 = [
            'id'=>$post_id
        ];
        $title = Posts::query()->where($where1)->value('title');
//        dd($title);
        $condition = $request->get('condition','title');
        if($request->isMethod('post')){
            # 要执行的代码
//            $roles = array('center','is_super_user');
            $query = PostsComment::query()->where($where)->latest();
            $keywords = $request->get('keywords',null);
            if ($keywords) {
                $query->where($condition, 'like', '%' . $keywords . '%');
            }
            $status = $request->get('status','all');
            if($status != 'all'){
                $query->where('status', $status);
            }
            $pageSize = $request->get('limit',10);
            # 计算offset值
            $offset = $request->get('offset',0);
            # 总数
            $total = $query->count();
            $data = $query->limit($pageSize)->offset($offset)->get();
            $result = array();
            if($data){
                foreach($data as $val){
                    $result[] = PostsComment::getDataByID($val['id']);
                }
            }
            return ret([
                'total'=>$total,
                'data'=>$result
            ]);
        }
        $admin_id = $this->admin_id;
        $subtitle = '帖子评论管理';
        return view('v1.admin.post.comment.index',compact('subtitle','admin_id','title','post_id'));
    }

    /**
     * 审核帖子评论
     *
     * @param Request $request
     * @param $id
     * @return Application|Factory|View|RedirectResponse
     */
    public function auditing(Request $request,$id)
    {
        $model = PostsComment::query();
        $where = [
            'id'=>$id
        ];
        $model_query = AdminModelQuery($id,$model,$where);
        if($model_query['code'] == 0){
            return  $this->retError($model_query['msg'],$request);
        }
        $comment = PostsComment::query()->where('id',$id)->value('comment');
        $cuser_id = PostsComment::query()->where('id',$id)->value('user_id');
        //var_dump($id);die;
        if($request->isMethod('post')) {
            $admin_id = $request->get('admin_id');
            $table = 'posts_comment';
            $validator = AdminFormVerify($table, $request, 'update');
            if ($validator->fails()) {
                return $this->retError($validator,$request);
            }
            $post = $request->all();
            unset($post['_token']);
            unset($post['id']);
            $update = $post;
            $status_update = $post['status'];
            $logs = [
                'admin_id' => $request->get('admin_id'),
                'operate_menu' => OperationalLogs::$operateItem['posts_manage'],
                'operate_item' => '评论内容：'.$comment,
                'operate_content' => OperationalLogs::$operate['postsComment_status_change'] . '为：' . PostsComment::getStatusArray()[$status_update],
            ];
            DB::beginTransaction();
            try {
                PostsComment::query()->where('id', $id)->update($update);
                //修改用户
                if($cuser_id){
                    Users::query()->where('id',$cuser_id)->update(['is_unread_review'=>1]);
                }

                #操作日志
                OperationalLogs::AddOperationalLogs($logs);
                DB::commit();
                return $this->retSuccess();
            } catch (\Exception $exception) {
                DB::rollback();
                return $this->retError($exception->getMessage(), $request);
            }
        }
        $url = route('v1.admin.post.comment.index');
        $params = [
            'url'=>$url,
            'success'=>'success'
        ];
        $subtitle = '帖子评论审核';
        $view = 'v1.admin.post.comment.auditing';
        $data = PostsComment::getDataByID($id);
        $compact = compact('subtitle','params','data',);
        return view($view,$compact);
    }

    /**
     * 删除帖子评论
     *
     * @param Request $request
     * @param $id
     * @return array
     * @throws \Throwable
     */
    public function delete(Request $request,$id): array
    {
        $where = [
            'id'=>$id,
        ];
        $data = PostsComment::query()->where($where);
        $logs = [
            'admin_id'=>$request->get('admin_id'),
            'operate_menu'=>OperationalLogs::$operateItem['posts_manage'],
            'operate_item'=>PostsComment::query()->where($where)->value('comment').'('.$id.')',
            'operate_content'=>OperationalLogs::$operate['postsComment_delete'],
        ];
        return AdminDeleteData($data,$logs);
    }
}
